home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / pascal / oop55.zip / BREAKOUT.PAS < prev    next >
Pascal/Delphi Source File  |  1989-05-02  |  7KB  |  302 lines

  1.  
  2. { Turbo Breakout }
  3. { Copyright (c) 1989 by Borland International, Inc. }
  4.  
  5. program Breakout;
  6. { Turbo Pascal 5.5 object-oriented example.
  7.  
  8.   This is a version of the classic arcade game, Breakout.
  9.  
  10.     SCREEN.PAS
  11.     COUNT.PAS
  12.     BRICKS.PAS
  13.     BOUNDS.PAS
  14.     WALLS.PAS
  15.     BREAKOUT.PAS
  16.  
  17.   To build an executable file, compile from the command line with:
  18.  
  19.     tpc /b breakout
  20.  
  21.   or load BREAKOUT.PAS into the integrated development
  22.   environment and press F9.
  23.  
  24.   When testing the program, you may want to force the paddle to
  25.   follow the ball, so you'll never miss. The program contains
  26.   conditional compilation directives to produce this version, and
  27.   you can build it from the command line with:
  28.  
  29.     tpc /DTest breakout
  30.  
  31.   or load BREAKOUT.PAS into the integrated development
  32.   environment, select Alt-O/C/C, type 'Test' (without the quotes,
  33.   of course) followed by the Enter key, then select Alt-C/B to
  34.   rebuild the executable file.
  35. }
  36.  
  37. uses Screen, Count, Bricks, Bounds, Walls, Crt, Dos;
  38.  
  39. var
  40.   ss        : SaveScreen;
  41.   w         : Wall;
  42.   b         : Ball;
  43.   p         : Paddle;
  44.   Speed     : LimitCounter;
  45.   Left      : LeftBound;
  46.   Top       : UpperBound;
  47.   Right     : RightBound;
  48.   Bottom    : LowerBound;
  49.   Obstacles : ObstacleList;
  50.   PaddleMsg,
  51.   SpeedMsg,
  52.   StartMsg,
  53.   QuitMsg,
  54.   PauseMsg1,
  55.   PauseMsg2,
  56.   TypeMsg   : TextString;
  57.   Score     : Counter;
  58.   Highest   : Counter;
  59.   Balls     : DownCounter;
  60.   X         : Integer;
  61.   Finished  : Boolean;
  62.   FirstGame : Boolean;
  63.   TypeInc,
  64.   ch        : Char;
  65.  
  66. procedure Startup;
  67. begin
  68.   { First set up the screen and the cursor }
  69.   ss.Init;
  70.   TextBackground(BLACK);
  71.   ClrScr;
  72.  
  73.   { Create the boundaries of the playfield }
  74.   Left.Init(0, 0, 27, False);
  75.   Top.Init(0, 0, 82, False);
  76.   Right.Init(81, 0, 27, False);
  77.   Bottom.Init(0, 24, 82, True);
  78.  
  79.   { Initialize the score displays }
  80.   Score.Init(0, 65, 24, 'Score', 15);
  81.   Score.Show;
  82.   Highest.Init(0, 60, 25, 'High Score', 14);
  83.   Highest.Show;
  84.  
  85.   { Set up the various menu messages }
  86.   PauseMsg1.Init(31, 18, 'Paused.  Press any', 15);
  87.   PauseMsg2.Init(31, 19, ' key to continue.', 15);
  88.   SpeedMsg.Init(5, 23, #24 + #25  + ' to change speed', 14);
  89.   StartMsg.Init(5, 24, #17 + #196 + #217 + ' to begin game', 14);
  90.   PaddleMsg.Init(5, 24, #27 + #26 + ' to move paddle', 14);
  91.   QuitMsg.Init(5, 25, 'ESC to quit', 14);
  92.   QuitMsg.Show;
  93.  
  94.   { Set up the information messages }
  95.   Balls.Init(5, 40, 24, -1, 'Balls', 15);
  96.   Balls.Show;
  97.   Speed.Init(1, 40, 25, 1, 10, 'Speed', 14);
  98.   Speed.Show;
  99.  
  100.   { Build the wall }
  101.   w.Init(1, 1, 16, 10);
  102.   w.Show;
  103.  
  104.   { Need to initialize these, even though we're going to move them later }
  105.   b.Init(10, 22, 1, -1, YELLOW);
  106.   p.Init(8, 23, WHITE);
  107.  
  108.   { Put the various obstacles into a list.  We don't really need
  109.     to do this, but it makes changing things around much easier }
  110.   Obstacles.Init;
  111.   Obstacles.Append(@p);
  112.   Obstacles.Append(@w);
  113.   Obstacles.Append(@Left);
  114.   Obstacles.Append(@Top);
  115.   Obstacles.Append(@Right);
  116.   Obstacles.Append(@Bottom);
  117.  
  118.   TypeMsg.Init(22, 12, 'Increase typematic rate? (y/n) ', WHITE);
  119.   TypeMsg.Show;
  120.   repeat
  121.     TypeInc := UpCase(ReadKey);
  122.   until (TypeInc = 'Y') or (TypeInc = 'N');
  123.   TypeMsg.Hide;
  124.  
  125.   if TypeInc = 'Y' then
  126.     ss.Speedup;
  127.  
  128.   ss.SetCursor($2000);
  129.   Randomize;
  130.   FirstGame := True;
  131. end;
  132.  
  133. procedure NewGame;
  134. begin
  135.   Balls.Reset;
  136.   Score.Reset;
  137.   if not FirstGame then
  138.     w.Reset;
  139.   X := Random(78) + 3;
  140.   b.MoveTo(X, 22);
  141.   p.MoveTo(X-2, 23);
  142.   b.Show;
  143.   p.Show;
  144.   Balls.Decrement;
  145.   FirstGame := False;
  146. end;
  147.  
  148. { This procedure handles keystrokes between games.
  149.   It returns False if the user presses ESC, otherwise it returns True. }
  150. function MainMenu : Boolean;
  151. var
  152.   Done : Boolean;
  153. begin
  154.   MainMenu := True;
  155.   Done := False;
  156.   SpeedMsg.Show;
  157.   StartMsg.Show;
  158.   while not Done do
  159.   begin
  160.     ch := ReadKey;
  161.     case ch of
  162.       Chr(27) :
  163.         begin
  164.           MainMenu := False;
  165.           Done := True;
  166.         end;
  167.       #13 : Done := True;
  168.       #0  :
  169.         begin
  170.           ch := ReadKey;
  171.           if Ord(ch) = 72 then
  172.             Speed.Increment
  173.           else if Ord(ch) = 80 then
  174.             Speed.Decrement;
  175.         end;
  176.       end;
  177.   end;
  178.   SpeedMsg.Hide;
  179.   StartMsg.Hide;
  180. end;
  181.  
  182. { This procedure handles keystrokes while the game is in progress }
  183. procedure ProcessKeyStroke;
  184.  
  185. { Pause the game }
  186. procedure Pause;
  187. begin
  188.   PauseMsg1.Show;
  189.   PauseMsg2.Show;
  190.   ch := ReadKey;
  191.   if KeyPressed then
  192.     ch := ReadKey;  { Swallow extended keystrokes }
  193.   PauseMsg1.Hide;
  194.   PauseMsg2.Hide;
  195.   b.Show;
  196. end;
  197.  
  198. begin
  199.   ch := ReadKey;
  200.   case ch of
  201.     Chr(27) : Finished := True;
  202.     Chr(0)    :
  203.       begin
  204.         ch := ReadKey;
  205. {$IFNDEF Test}
  206.         case Ord(ch) of
  207.           75: p.MoveTo(p.GetX - 1, p.GetY);  { Left Arrow }
  208.           77: p.MoveTo(p.GetX + 1, p.GetY);  { Right Arrow }
  209.         else
  210.           Pause;
  211.         end;
  212. {$ELSE}
  213.         Pause;
  214. {$ENDIF}
  215.       end
  216.     else
  217.       Pause;
  218.   end;
  219. end;
  220.  
  221. { This procedure checks for collisions with any of the obstacles
  222.   and updates the screen accordingly. }
  223. procedure Update;
  224. var
  225.   Offset : Integer;
  226. begin
  227.   if Obstacles.CheckCollisions(b, Score) then
  228.   begin
  229.     b.MoveY;
  230.     p.MoveTo(b.GetX - 2, p.GetY);
  231.     sound(150);
  232.     Delay(300);
  233.     nosound;
  234.     Balls.Decrement;
  235.     while KeyPressed do
  236.       ch := ReadKey;
  237.   end;
  238.  
  239.   b.MoveX;
  240.   b.MoveY;
  241.  
  242. {$IFDEF Test}
  243.   p.MoveTo(b.NextX -2, p.GetY);
  244. {$ENDIF}
  245. end;
  246.  
  247. { This procedure cleans up when we're exiting from the program }
  248. procedure ShutDown;
  249. begin
  250.  b.Hide;
  251.  Obstacles.Hide;
  252.  Balls.Hide;
  253.  Score.Hide;
  254.  
  255.  Obstacles.Done;
  256.  
  257.  ss.Restore;
  258.  if TypeInc = 'Y' then
  259.    ss.Slowdown;
  260.  ClrScr;
  261. end;
  262.  
  263. { This procedure plays a game.  The main loop allows up to ten keystrokes,
  264.  then moves the ball and checks for collisions }
  265. procedure Play;
  266. var
  267.  KeyLoops : Integer;
  268. begin
  269.  NewGame;
  270. {$IFNDEF Test}
  271.   PaddleMsg.Show;
  272. {$ENDIF}
  273.   Finished := False;
  274.   KeyLoops := 0;
  275.   repeat
  276.     if KeyPressed then
  277.       ProcessKeyStroke;
  278.     Inc(KeyLoops);
  279.     if (KeyLoops = 10) and not Finished then
  280.     begin
  281.       KeyLoops := 0;
  282.       UpDate;
  283.     end;
  284.     Delay(12 - Speed.GetValue);
  285.   until Finished or Balls.Last;
  286.   PaddleMsg.Hide;
  287. end;
  288.  
  289. begin
  290.   Startup;
  291.   while MainMenu do
  292.   begin
  293.     Play;
  294.     Balls.Reset;
  295.     b.Hide;
  296.     p.Hide;
  297.     if Score.GetValue > Highest.GetValue then
  298.       Highest.SetValue(Score.GetValue);
  299.   end;
  300.   ShutDown;
  301. end.
  302.